package chapter2;

import java.util.Stack;

/**
 * @author Hang
 * @date 2022/8/7
 * @project AutumnOffer
 * @description 面试题6
 **/
public class reversePrint {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(3);
        head.next.next = new ListNode(2);
        for (int i:reversePrint(head)) {
            System.out.println(i);
        }
    }
    public static int[] reversePrint(ListNode head){
        Stack<Integer> st = new Stack<>(); // 使用栈将数据存放
        while(head != null){
            st.push(head.val);
            head = head.next;
        }
        int size = st.size(); // 根据栈的大小定义数组
        int[] res = new int[size];
        for(int i=0;i<size;i++){
            res[i] = st.pop(); // 依次出队放入数组中
        }
        return res;
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
 }